<?php
namespace Zwei\Retry;

use Zwei\Retry\Exception\RetryException;

class RetryHelper {

    /**
     * 快速重试毫秒级
     *
     * @param callable $callFunc
     * @return Retry
     */
    public static function fastRetry(callable $callFunc)
    {
        return self::retry(3, 5000, $callFunc);
    }

    /**
     * @param int $maxRetry 最大重试次数
     * @param integer $delayMicroseconds 重试间隔微秒数
     * @param callable $callFunc
     * @return Retry
     */
    public static function retry($maxRetry, $delayMicroseconds, callable $callFunc)
    {
        $static = new static();
        $retry = new Retry($maxRetry, $delayMicroseconds);
        try {
            $result = $callFunc();
            $retry->setIsCalled(true, true, $result);
        } catch (RetryException $e) {
            $retry->execute($callFunc);
        }
        return $retry;
    }

    public static function retryThrowException($message, $code = 0) {
        throw new RetryException($message, $code);
    }
}